use std::marker::PhantomData;
use std::ops::Deref;
struct Cacher<T, L, U>
where
    T: Fn(L) -> U,
{
    closure: T,
    value: Option<U>,
    _marker: PhantomData<(L, U)>, // 保留类型参数 L 和 U
}

impl<T, L, U: Clone> Cacher<T, L, U>
where
    T: Fn(L) -> U,
{
    /// 创建一个新的 Cacher 实例
    fn new(closure: T) -> Self {
        Cacher {
            closure,
            value: None,
            _marker: PhantomData,
        }
    }

    /// 获取或计算并缓存值
    fn value(&mut self, arg: L) -> U {
        let mut cached_value = self.value.take(); // 临时存储 `self.value`
        match cached_value {
            Some(v) => v,
            None => {
                let v = (self.closure)(arg);
                self.value = Some(v.clone());
                v
            }
        }
    }
}
struct MyBox<T>(T);
impl<T> MyBox<T> {
    fn new(arg: T) -> MyBox<T> {
        MyBox(arg)
    }
}
impl<T> Deref for MyBox<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
fn main() {
    let x = 5;
    let y = &x;
    // assert_eq!(5, x);
    // assert_eq!(*y, 5);
    let x1 = 5;
    let y1 = MyBox::new(x1);

    println!("y1{}", *y1.deref());

    let m: MyBox<String> = MyBox::new(String::from("Rust"));
    hello(&(*m)[..]);
    let str = String::from("Hello!");
    let new_str: &str = &str[..3];
    println!("new_str:{}", new_str);
}
fn hello(name: &str) {
    println!("Hello,{}!", name);
}
